package logic

import (
	"context"
	"errors"
	"taobao-merchandise/models/mysql"

	"taobao-merchandise/taobaorpc/internal/svc"
	"taobao-merchandise/taobaorpc/taobaorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsDelLogic {
	return &GoodsDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsDelLogic) GoodsDel(in *taobaorpc.GoodsDelRequest) (*taobaorpc.GoodsDelResponse, error) {
	// todo: add your logic here and delete this line

	var goods mysql.Goods

	if err := goods.FindGoodsById(mysql.DB, in.GoodsId); err != nil {
		return nil, errors.New("商品不存在")
	}

	if in.UserId != goods.UserId {
		return nil, errors.New("当前商品不是自己的，不允许删除")
	}

	if err := goods.GoodsDel(mysql.DB, in.GoodsId); err != nil {
		return nil, err
	}

	return &taobaorpc.GoodsDelResponse{}, nil
}
